//
//  SessionManager.m
//  ShanMei
//
//  Created by huazi on 15/4/12.
//  Copyright (c) 2015年 huazi. All rights reserved.
//

#import "SessionManager.h"

@implementation SessionManager
+(SessionManager *)shareSessionManager {
    static SessionManager *_sessionManager =nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sessionManager =[[SessionManager alloc] init];
    });
    return _sessionManager;
}
/*
- (instancetype)init {
    if (self =[super init]) {
        MessageDatabaseManager *_messageDatabase =[MessageDatabaseManager shareMessageDatabaseManager];
        NSString *strCreateTableSESSION =@"CREATE TABLE IF NOT EXISTS SESSION ( oprId TEXT, userId TEXT,photoUrl TEXT,nickName TEXT, content TEXT, sendTime TEXT)";
        [_messageDatabase execSql:strCreateTableSESSION];
    }
    return self;
}
-(void)insertOrReplaceSession:(Session *)_session {
    MessageDatabaseManager *_messageDatabase =[MessageDatabaseManager shareMessageDatabaseManager];
    Person *_person =[Person sharePerson];
    NSString *strQuerySessionExist =[NSString stringWithFormat: @"SELECT count(*) FROM SESSION WHERE (SESSION.oprId ='%@' AND SESSION.userId='%@')",_person.userId,_session.userId];
    NSInteger isExist =[_messageDatabase querySessionTableExistsession:strQuerySessionExist];
    if (isExist) {
        [self replaceSession:_session];
    }
    else {
        [self insertSession:_session];
    }
    
}
//oprId TEXT, userId TEXT,photoUrl TEXT,nickName TEXT, content TEXT, sendTime TEXT
- (void)insertSession:(Session *)_session {
    MessageDatabaseManager *SQLManager =[MessageDatabaseManager shareMessageDatabaseManager];
    Person *_person =[Person sharePerson];
    NSString *strINSRETINTO =[NSString stringWithFormat:@"INSERT INTO SESSION (oprId ,userId , photoUrl ,nickName ,content ,sendTime) VALUES(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",_person.userId,_session.userId,_session.photoUrl,_session.nickName,_session.content,_session.sendTime];
    [SQLManager execSql:strINSRETINTO];
}
- (void)replaceSession:(Session *)_session {
    Person *_person =[Person sharePerson];
    MessageDatabaseManager *SQLManager =[MessageDatabaseManager shareMessageDatabaseManager];
    NSString *strAlter =[NSString stringWithFormat:@"UPDATE SESSION SET sendTime='%@',content='%@',nickName='%@',photoUrl='%@' WHERE (oprId='%@' AND userId='%@')",_session.sendTime,_session.content,_session.nickName,_session.photoUrl,_person.userId,_session.userId];
    [SQLManager execSql:strAlter];
}
-(void)deleteNewsFromSessionTable:(Session *)news {
    Person *_person =[Person sharePerson];
    MessageDatabaseManager *SQLManager =[MessageDatabaseManager shareMessageDatabaseManager];
    Person *person= [Person sharePerson];
    NSString * strDelete =[NSString stringWithFormat:@"DELETE FROM SESSION WHERE oprID ='%@' AND  userId='%@' ",person.userId,news.userId];
    [SQLManager execSql:strDelete];
}
 */
@end
